export type RState = {
  count: number;
};
export type RAction = {
  type: "add" | "sub" | "reset"; // 操作类型
  payload?: number; // 操作数
};

export const reducer = (state: RState, action: RAction) => {
  switch (action.type) {
    case "add":
      return { count: state.count + 1 };
    case "sub":
      return { count: state.count > 0 ? state.count - 1 : 0 };
    case "reset":
      return { count: action.payload ? action.payload : 0 };
  }
};
